package org.example.client.veri.bind;


import lombok.extern.slf4j.Slf4j;
import org.example.common.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * @author mojito
 * @Classname ServiceProxy
 * @Description TODO
 * @Date 2020/7/20 下午9:57
 **/
@Slf4j
public class ServiceProxy<T> implements InvocationHandler {
    private Class<T> face;
    private Client client;

    public ServiceProxy(Class<T> face, Client client) {
        this.face = face;
        this.client = client;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RpcRequest request = new RpcRequest();
        request.setRequestId(UUID.randomUUID().toString());
        request.setClassName(face.getName());
        request.setMethodName(method.getName());
        request.setParameterTypes(method.getParameterTypes());
        request.setParams(args);
        log.info("rpc calling: [{}]" + request);
        Object process = client.process(request);
        log.info("dynamic response:{}", process);
        return process;
    }
}
